Create Your Own Temperature Controller with mLink – Part One

In this project we’ll show you how to create your very own temperature controller using an Arduino board and some mLink I2C modules. We’ll break this project into two parts with the second part being optional depending on how complex you’d like your controller to be.

In the first part we’ll use a mLink NTC temperature sensor and a mLink relay module connected to an Arduino. You can use any Arduino compatible development board with this project so long as it has an I2C port and can supply 5V to the mLink modules. For this project I’ll be using an Uno plus.

The video does make some assumptions, for instance, that you already have a copy of the Arduino development environment (IDE) installed and you already know how to use it to program your development board with a sketch. It also assumes that you have some basic understanding of the C++ programming language used by the IDE.

So, for step one, you’ll need the following…

An Arduino compatible development board with I2C and 5V out. As previously mentioned, I’m using the Uno Plus.

The mLink NTC temperature sensor.

The mLink 1 channel relay module.

Some Dupont cables to connect the modules together. I’m using this 40 way dupont cable that I’ve just stripped down to 4 – if you want something longer, we do have 20cm long ones. Obviously, the cables you need will depend on the headers on your specific development board.

And finally, if you intend to complete the second of these videos, you’ll need the mLink 1602 or 2004 LCD. The project will work with both sizes, so it’s personal choice.

So here’s Part One…

Connections diagram

Installing the Library

We now have a working temperature controller that can switch a heating or cooling device on and off. Set the onSetpoint and offSetpoint variables to whatever temperatures you require, upload the sketch and give it a try! If you get any errors, or your sketch doesn’t work correctly check the code below.

Arduino Sketch

/* FILE:    mLink_Temp_Controller_Part_1.ino
   DATE:    14/02/24
   VERSION: 1.0
   AUTHOR:  Anita Davies
   

This sketch uses the mLink library to control a mLink 5V relay (SKU:HCMODU0182) and 
mLink NTC Temperature Sensor (SKU:HCMODU0186).

Please see Licence.txt in the library folder for terms of use.
*/



#include "mLink.h"              // Include the library

#define RLY_ADD   0x52          // Default I2C address of Relay
#define NTC_ADD   0x54          // Default I2C address of Temp Sensor

float onSetpoint = 26.0;        // Default ON temperature
float offSetpoint = 28.0;       // Default OFF temperature

boolean relayState = OFF;       // Global variable stores the relay state

mLink mLink;                    // Create an instance of the library

// Initial setup - the code here will run once

void setup()
{
  mLink.init();                 // Initialise the library

  Serial.begin(9600);           // Set data rate in bits for serial transmission
}

void loop()
{
  float temp = getTemp();       // Read current temperature

  updateRelay(temp);            // Get relay status

  printStatus(temp);            // Print

  delay(500);                   // Half second delay
}

// getTemp function. Runs read command 10 times. Adds result to "temp" variable.
//Divides sum by 10 to get average before returning result.

float getTemp()                 
{
  float temp = 0;

  for(byte i = 0; i < 10; i++)
    temp += mLink.NTC_Temp(NTC_ADD);

  temp /= 10;

  return temp;
}

// printStatus function

void printStatus(float temp)
{
  Serial.print("Temperature: ");    // Prints word Temperature: to serial monitor
  Serial.print(temp, 1);            // Prints temperature to one decimal place
  Serial.print("oC, Status: ");     // Prints centigrade sign to serial monitor

  if(relayState)
    Serial.println("ON");           // Prints ON or OFF depending on if/else statement
  else
    Serial.println("OFF");
}

// updateRelay function

void updateRelay(float temp)
{

//For the heating condition we want to turn the relay on when the temperature
//is below the on setpoint and turn the relay off when it is above the off setpoint

  if(onSetpoint < offSetpoint)
  {
    if(temp < onSetpoint)
      relayState = ON;
    else if(temp > offSetpoint)
      relayState = OFF;
  }


//And for the cooling condition we need to turn on the relay if the temperature
//is above the on setpoint and turn it off when it is below the off setpoint

    else if(offSetpoint < onSetpoint)
    {
      if(temp > onSetpoint)
        relayState = ON;
      else if(temp < offSetpoint)
        relayState = OFF;
    }

    mLink.SET_RLY0(RLY_ADD, relayState);  // Update relayState variable
}

Leave a Reply

Your email address will not be published. Required fields are marked *